#include<iostream>
#include <string>
#include <queue>
using namespace std;
typedef struct node{
    int data;
    struct node *lchild;
    struct node *rchild;
}BTNode;
typedef struct node *BTree;
BTree CreateBT(int *end,int *in,int n);
void PreOrder( BTree BT );
int main()
{
	BTree BT;
	int n,i;
	cin>>n;
	int end[n];
	int in[n];
	for(i=0;i<n;i++)
	cin>>end[i];
	for(i=0;i<n;i++)
	cin>>in[i];
	BT=CreateBT(end,in,n);
	cout<<"Preorder:";
	PreOrder( BT );
 } 
 BTree CreateBT(int *end,int *in,int n){
 	BTree b;
	int *p;
	int k;
	if(n<=0)return NULL;
	b=new BTNode;
	b->data=*(end+n-1);
	for(p=in;p<in+n;p++)
		if(*p==*(end+n-1))
			break;
	k=p-in;
	b->rchild=CreateBT(end+k,in+k+1,n-k-1);
	b->lchild=CreateBT(end,in,k);
	return b;
 }
 void PreOrder( BTree BT ){
	static int flag=0;
	if(BT!=NULL){
		if(flag==0)printf("%d",BT->data),flag=1;
	    else printf(" %d",BT->data);
	    PreOrder(BT->lchild);
	    PreOrder(BT->rchild);
  }
} 



//7
//2 3 1 5 7 6 4
//1 2 3 4 5 6 7 
